package com.snake.springboot.custom.service.impl.goods;

import com.github.pagehelper.Page;
import com.snake.springboot.custom.api.model.goods.StrategyGoodsModel;
import com.snake.springboot.custom.api.model.goods.StrategyGoodsStateModel;
import com.snake.springboot.custom.api.request.goods.StrategyGoodsQuery;
import com.snake.springboot.custom.api.request.page.PageQuery;
import com.snake.springboot.custom.api.response.goods.StrategyGoodsRsp;
import com.snake.springboot.custom.api.service.goods.StrategyGoodsService;
import com.snake.springboot.custom.api.service.goods.StryGoodsDetailService;
import com.snake.springboot.custom.dao.mybatis.entity.goods.StrategyGoodsDO;
import com.snake.springboot.custom.dao.mybatis.mappers.goods.StrategyGoodsMapper;
import com.snake.springboot.custom.service.assemble.goods.StrategyGoodsAssembler;
import com.snake.springboot.custom.service.utils.PageUtils;
import org.snake.common.result.ReturnList;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StrategyGoodsServiceImpl implements StrategyGoodsService{

    private StrategyGoodsMapper strategyGoodsMapper;
    private StryGoodsDetailService detailService;
    @Autowired
    public StrategyGoodsServiceImpl(StrategyGoodsMapper strategyGoodsMapper, StryGoodsDetailService detailService) {
        this.strategyGoodsMapper = strategyGoodsMapper;
        this.detailService = detailService;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StrategyGoodsModel addStrategyGoods(StrategyGoodsModel model) {
        StrategyGoodsDO goodsDO = StrategyGoodsAssembler.toGoodsDO(model,true);
        strategyGoodsMapper.insert(goodsDO);
        detailService.addDetail(model);
        return model;
    }

    @Override
    public void modifyStrategyGoods(StrategyGoodsModel model) {
        StrategyGoodsDO goodsDO = StrategyGoodsAssembler.toGoodsDO(model,false);
        strategyGoodsMapper.update(goodsDO);
        detailService.addDetail(model);
    }

    @Override
    public void removeStrategyGoods(String id) {
        strategyGoodsMapper.delete(id);
        detailService.delByParentId(id);
    }

    @Override
    public ReturnList<StrategyGoodsModel> queryStrategyGoodsList(PageQuery<StrategyGoodsQuery> params) {
        Page<Object> objects = PageUtils.forcePage(params.getPage());
        StrategyGoodsDO queryDO = StrategyGoodsAssembler.toGoodsDO(params.getParams());
        List<StrategyGoodsDO> goodsDOList = strategyGoodsMapper.queryList(queryDO);
        List<StrategyGoodsModel> goodsModels = ListUtils.copy(goodsDOList, StrategyGoodsModel.class);
        return new ReturnList<>(goodsModels,objects.getTotal());
    }

    @Override
    public StrategyGoodsRsp detail(String id) {
        StrategyGoodsDO goodsDO = strategyGoodsMapper.queryById(id);
        if (goodsDO == null){
            return null;
        }
        StrategyGoodsRsp rsp = StrategyGoodsAssembler.toGoodsRsp(goodsDO);
        detailService.queryDetail(ListUtils.initObjList(rsp));
        return rsp;
    }

    @Override
    public void upState(StrategyGoodsStateModel model) {
        StrategyGoodsDO goodsDO = StrategyGoodsAssembler.toGoodsDO(model);
        strategyGoodsMapper.upState(goodsDO);
    }

    @Override
    public List<StrategyGoodsRsp> payDetail(List<String> goodsIds) {
        List<StrategyGoodsDO> goodsDOList = strategyGoodsMapper.payDetail(goodsIds);
        List<StrategyGoodsRsp> goodsRsp = ListUtils.copy(goodsDOList, StrategyGoodsRsp.class);
        detailService.queryDetail(goodsRsp);
        return goodsRsp;
    }
}
